<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Chapter extends Model
{
    protected $table = 'chapter';

    protected $guarded = [];


//    public $timestamps = false;

    public function scopeNormal($query){

        return $query->where([

            'status' => 1,
            'type' => 0,
            'examine' => 1

        ]);

    }


    public function article(){

        return $this->hasOne(Article::class,'chapter_num','chapter_num');

    }

    public function book(){

        return $this->belongsTo(Book::class,'book_num','book_num');


    }

    public function schedule(){

        return $this->morphOne(Schedule::class,'scheduleable');

    }


    protected static function boot()
    {
        parent::boot();

        static::creating(function ($chapter){
            $chapter->chapter_num = md5(time().$chapter->name);
            $chapter->create_time = time();
            $chapter->last_modify_time = time();
            $chapter->synced_at = Carbon::now();
            $chapter->delete_time = 0;
            $chapter->examine = 2;
        });


    }


}
